package com.spa.infrastructure.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.ShopAddCommand;
import com.spa.application.command.admin.ShopEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.AllShopDTO;
import com.spa.application.dto.admin.ListShopDTO;
import com.spa.application.dto.admin.ShopDTO;
import com.spa.application.dto.admin.ShopExportDTO;
import com.spa.application.dto.common.rule.RuleDto;
import com.spa.application.dto.massagist.MassagistShopDTO;
import com.spa.application.query.admin.ShopQuery;
import com.spa.domain.entity.Rule;
import com.spa.domain.entity.Shop;
import com.spa.infrastructure.repository.condition.ShopCondition;
import com.spa.infrastructure.repository.po.ShopPO;
import org.mapstruct.Mapper;
import org.mapstruct.MapperConfig;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
@MapperConfig(unmappedTargetPolicy = ReportingPolicy.WARN)
public interface ShopMapperStruct {

    ShopMapperStruct INSTANCE = Mappers.getMapper(ShopMapperStruct.class);

    Shop po2Entity(ShopPO po);

    ShopPO command2Po(ShopAddCommand command);

    ShopPO command2Po(ShopEditCommand command);

    Page<Shop> po2Entity4Page(Page<ShopPO> po);

    List<Shop> po2Entity4List(List<ShopPO> po);

    PageResult<ShopDTO> entity2ShopDto4Page(Page<Shop> entity);

    List<ListShopDTO> entity2ListShopDto4List(List<Shop> entity);

    MassagistShopDTO entity2messagistShopDto(Shop entity);

    @Mapping(source = "city.cityName", target = "cityName")
    @Mapping(source = "city.id", target = "cityId")
    @Mapping(source = "ruleList", target = "ruleDtoList", qualifiedByName = "ruleList2DtoList")
    ShopDTO entity2ShopDto(Shop entity);

    ShopCondition query2Condition(ShopQuery query);

    /**
     * 该方法将Shop实体对象转换为ShopExportDTO数据传输对象。
     *
     * @param entity Shop实体，包含城市信息（嵌套属性city）
     * @return 转换后的ShopExportDTO对象，其中包含了与源Shop实体中city相关联的城市名称和城市ID信息
     * @Mapping(source = "city.cityName", target = "cityName") 表示从源对象的嵌套属性city的城市名称映射到目标对象的城市名称字段
     * @Mapping(source = "city.id", target = "cityId") 表示从源对象的嵌套属性city的ID映射到目标对象的城市ID字段
     */
    @Mapping(source = "city.cityName", target = "cityName")
    @Mapping(source = "city.id", target = "cityId")
    @Mapping(source = "ruleList", target = "ruleDtoList", qualifiedByName = "ruleList2DtoList")
    ShopExportDTO entity2ShopExportDto(Shop entity);

    List<ShopExportDTO> entity2ShopExportDto(List<Shop> shopList);

    List<AllShopDTO> entity2AllShopDTO(List<Shop> shopList);

    @Named("ruleList2DtoList")
    default List<RuleDto> ruleList2DtoList(List<Rule> ruleList) {
        return Rule.listRuleToDto(ruleList);
    }
}
